from 第15天.class_decorator_with_param import RunTimeWithParam
from 第15天.class_decortor import RunTime
from 第15天.func_decorator import run_time
from 第15天.func_decorator_with_param import rum_time_with_param


def is_prime(num):
    """
    是否是一个质数（它的因数，只有它自己和 1），2，3，4
    :param num:
    :return:
    """
    if not isinstance(num, int):
        return False
    if num < 2:
        return False
    elif num == 2:
        return True
    else:
        for i in range(2, num):
            if num % i == 0:
                return False
        return True


def count_prime_nums(num):
    """
    统计1-num中质数的个数
    :param num:
    :return:
    """
    total_count = 0
    for i in range(1, num + 1):
        if is_prime(i):
            total_count += 1
    return total_count


@run_time
def count_prime_nums_with_func_decorator(nums):
    return count_prime_nums(nums)


@RunTime
def count_prime_nums_with_class_decorator(nums):
    return count_prime_nums(nums)


@rum_time_with_param("函数装饰器的参数")
def count_prime_nums_with_func_decorator_and_param(nums):
    return count_prime_nums(nums)


@RunTimeWithParam("类装饰器的参数")
def count_prime_num_with_class_decorator_and_param(nums):
    return count_prime_nums(nums)


if __name__ == "__main__":
    num_10000 = 10000

    # # 1.
    # print("1、调用装饰器的语法糖")
    # result_num_1 = count_prime_nums_with_func_decorator(num_10000)
    # print(result_num_1)
    #
    # # 2.
    # print("2、调用装饰器函数本身")
    # result_num_2 = run_time(count_prime_nums)(num_10000)
    # print(result_num_2)
    #
    # # 3
    # print("3、拆分调用装饰器函数的步骤")
    # result_num_3_run_time = run_time(count_prime_nums)
    # result_num_3 = result_num_3_run_time(num_10000)
    # print(result_num_3)

    # # 4
    # print("4、调用类装饰器的语法糖")
    # result_num_4 = count_prime_nums_with_class_decorator(num_10000)
    #
    # # 5
    # print("5、调用装饰器类本身")
    # result_num_5 = RunTime(count_prime_nums)(num_10000)
    #
    # # 6
    # print("6、拆分调用装饰器类的步骤")
    # result_num_6_run_time = RunTime(count_prime_nums)
    # result_num_6 = result_num_6_run_time(num_10000)

    # # 7
    # print("7、带参数的装饰器")
    # result_num_7 = count_prime_nums_with_func_decorator_and_param(num_10000)
    #
    # # 8
    # print("8、调用带参数的装饰器本身")
    # result_num_8_1 = rum_time_with_param("自定义的函数装饰器参数")
    # result_num_8_2 = result_num_8_1(count_prime_nums)
    # result_num_8 = result_num_8_2(num_10000)

    # 9
    print("9、带参数的装饰器")
    result_num_9 = count_prime_num_with_class_decorator_and_param(num_10000)

    # 10
    print("10、调用带参数的装饰器本身")
    result_num_10_1 = RunTimeWithParam("自定义的类装饰器参数")
    result_num_10_2 = result_num_10_1(count_prime_nums)
    result_num_10 = result_num_10_2(num_10000)
